const LApp = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    ProductModel: {},
    isEdit: '',
    areaIndex: 0,
    verIndex:0,
    VersionList:[{Code:0,Name:'标准'},{Code:1,Name:'黄金'},{Code:2,Name:'铂金'},{Code:3,Name:'钻石'}]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },



  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.getEnterpriseSelectItemList();
    this.data.ProductModel.VersionName = this.data.VersionList[0].Name;
    this.data.ProductModel.Version = this.data.VersionList[0].Code;   
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  //绑定下拉框
  getEnterpriseSelectItemList: function () {
    let _this = this;
    LApp._get({
      url: "nbapi/product/enterselectitem",
      data: { enterpriseId: LApp.globalData.userInfo.EnterpriseId },
      success: function (r) {
        _this.data.ProductModel.EnterpriseId = r[0].Code;
        _this.data.ProductModel.EnterpriseName = r[0].Name;
        _this.data.EnterpriseSelectItem = r;
        _this.GetProductSelectItemList(_this.data.ProductModel.EnterpriseId);
        _this.setData(_this.data);
      }
    })
  },
  //下拉框选值
  EnterpriseNameSelect: function (e) {
    this.setData({
      areaIndex: e.detail.value
    });
    this.data.ProductModel.EnterpriseName = this.data.EnterpriseSelectItem[this.data.areaIndex].Name;
    this.data.ProductModel.EnterpriseId = this.data.EnterpriseSelectItem[this.data.areaIndex].Code;
    this.GetProductSelectItemList(this.data.ProductModel.EnterpriseId );
    this.setData(this.data)
  },
  //获取产品下拉
  GetProductSelectItemList: function (eId) {
    var _this = this;
    LApp._get({
      url: 'nbapi/product/selectitem',
      data: { enterpriseId: eId },
      success: function (r) {
       
         _this.data.ProductSelectItem = r;
         _this.setData(_this.data);
      }
    })
  },
  //产品下拉选择
  ProductNameSelect: function (e) {
   if (this.data.ProductSelectItem.length > 0) {
    this.data.proIndex = e.detail.value;
    this.data.ProductModel.ProductName = this.data.ProductSelectItem[this.data.proIndex].Name;
    this.data.ProductModel.ProductId = this.data.ProductSelectItem[this.data.proIndex].Code;
    this.setData(this.data)
   }
  },
  //版本选择
  VersionSelect:function(e){
      this.data.verIndex = e.detail.value;
      this.data.ProductModel.VersionName = this.data.VersionList[this.data.verIndex].Name;
      this.data.ProductModel.Version = this. data.VersionList[this.data.verIndex].Code;   
      this.setData(this.data);
  },
  //页面数据绑定
  bindModelChange: function (e) {
    wx.$$apply(e, this.data.ProductModel);
  },
  saveProductBatch: function (e) {
    this.data.ProductModel
    //批次名必输入
    if (!this.data.ProductModel.BatchName){
      wx.showToast({
        icon:'none',
        title: '请输入批次名称',
        mask:true
      })
      return;
    }
    //产品必选
    if(!this.data.ProductModel.ProductId){
       wx.showToast({
         icon: 'none',
         title: '请选择产品',
         mask: true
       })
       return;
    }
    LApp._post({
      url: "nbapi/productbatch/addbatch",
      data: this.data.ProductModel,
      success: function (r) {
        wx.showToast(
          {
            title: "添加成功！",
            icon: "success",
            showCancel: false,
            //刷新父级页面
            success: function () {
              wx.$$getParent().data.productBatchList = [];
              wx.$$getParent().getProductBatchList();
              wx.navigateBack({
                delta: 1
              })
            }
          })
      }
    })
  }
})
